package com.domino.client.mvc.impl.ai;

import java.util.Date;
import java.util.Iterator;
import java.util.Random;

import com.domino.shared.Player;
import com.domino.shared.PlayerId;
import com.domino.shared.model.Board;
import com.domino.shared.model.Brick;
import com.domino.shared.model.Color;
import com.domino.shared.model.Point;

public class Jutta implements Bot {

	private Player player = new Player(new PlayerId("client-bot"), "Jutta", null);
	private Color botColor;
	private Random rand = new Random(new Date().getTime());

	public Jutta(Color botColor) {
		this.botColor = botColor;
	}

	@Override
	public Player getPlayer() {
		return player;
	}

	@Override
	public Brick getNexMove(Board board) {
		Iterator<Point> iterator = board.getAvailblePoints(botColor).iterator();
		if (iterator.hasNext()==false) { 
			throw new IllegalStateException(); 
		}
		Point nextPoint = iterator.next();
		// There is a chance of trying to taking next entry
		// so that we get geometric probability for each available point
		int size = board.getAvailblePoints(botColor).size();
		int prob = (int)(100f*(1f-1f/size));
		while(iterator.hasNext() && rand.nextInt(100)<prob) {
			nextPoint = iterator.next();
		}
		return new Brick(nextPoint, botColor);
	}

}
